package cn.com.ghy.android.widget.visit;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import cn.com.ghy.android.bean.CompeteBrandInfoBean;
import cn.com.ghy.android.bean.DataCollectCompeteInfoBean;
import cn.com.ghy.android.bean.GoodsTypeBean;
import cn.com.ghy.android.bean.UnitInfoBean;
import cn.com.ghy.android.util.CommonUtil;
import cn.com.ghy.android.widget.R;

public class DataCollectCompete extends Activity {

	private Spinner spiCompeteBrand = null;
	private Spinner spiGoodsType = null;
	private Spinner spiUnit = null;
	private EditText txtShaJiaSKUNum = null;
	private EditText txtDisplayZb = null;
	private EditText txtJPSKUId = null;
	private EditText txtSKUId = null;
	private EditText txtSale = null;
	private EditText txtCuXSale = null;
	private EditText txtCompeteInfo = null;

	private Button btnStartDateModify = null;
	private Button btnEndDataModify = null;
	private Button btnSave = null;
	private Button btnBack = null;

	private Intent intent = null;
	private Bundle bundle = null;

	private List<CompeteBrandInfoBean> competeBrandInfoList = null;
	private List<GoodsTypeBean> goodsTypeList = null;
	private List<UnitInfoBean> unitInfoList = null;
	private String vsdate = null;
	private DataCollectCompeteInfoBean dataCollectCompeteInfo = null;
	private int mYear = 2011;
	private int mMonth = 0;
	private int mDay = 01;
	private int dataCollCompetePosition = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.data_collect_compete);
		getSession();
		findView();
		init();
		addListener();
	}

	@SuppressWarnings("unchecked")
	private void getSession() {
		bundle = this.getIntent().getExtras();
		competeBrandInfoList = (List<CompeteBrandInfoBean>) bundle
				.getSerializable("competeBrandInfoList");
		goodsTypeList = (List<GoodsTypeBean>) bundle.getSerializable("goodsTypeList");
		unitInfoList = (List<UnitInfoBean>) bundle.getSerializable("unitInfoList");
		dataCollectCompeteInfo = (DataCollectCompeteInfoBean) bundle
				.getSerializable("dataCollectCompeteInfo");
		vsdate = bundle.getString("vsdate");
		dataCollCompetePosition = bundle.getInt("dataCollCompetePosition", -1);
	}

	private void findView() {
		spiCompeteBrand = (Spinner) this.findViewById(R.id.spiCompeteBrand);
		spiGoodsType = (Spinner) this.findViewById(R.id.spiGoodsType);
		spiUnit = (Spinner) this.findViewById(R.id.spiUnit);
		txtShaJiaSKUNum = (EditText) this.findViewById(R.id.txtShaJiaSKUNum);
		txtDisplayZb = (EditText) this.findViewById(R.id.txtDisplayZb);
		txtJPSKUId = (EditText) this.findViewById(R.id.txtJPSKUId);
		txtSKUId = (EditText) this.findViewById(R.id.txtSKUId);
		txtSale = (EditText) this.findViewById(R.id.txtSale);
		txtCuXSale = (EditText) this.findViewById(R.id.txtCuXSale);
		txtCompeteInfo = (EditText) this.findViewById(R.id.txtCompeteInfo);

		btnStartDateModify = (Button) this.findViewById(R.id.btnStartDateModify);
		btnEndDataModify = (Button) this.findViewById(R.id.btnEndDataModify);
		btnSave = (Button) this.findViewById(R.id.btnSave);
		btnBack = (Button) this.findViewById(R.id.btnBack);
	}

	private void init() {
		ArrayAdapter<UnitInfoBean> unitInfoAdapter = new ArrayAdapter<UnitInfoBean>(
				DataCollectCompete.this, android.R.layout.simple_spinner_item, unitInfoList);
		unitInfoAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spiUnit.setAdapter(unitInfoAdapter);

		ArrayAdapter<CompeteBrandInfoBean> competeBrandAdapter = new ArrayAdapter<CompeteBrandInfoBean>(
				DataCollectCompete.this, android.R.layout.simple_spinner_item, competeBrandInfoList);
		competeBrandAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spiCompeteBrand.setAdapter(competeBrandAdapter);

		ArrayAdapter<GoodsTypeBean> goodsTypeAdapter = new ArrayAdapter<GoodsTypeBean>(
				DataCollectCompete.this, android.R.layout.simple_spinner_item, goodsTypeList);
		goodsTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spiGoodsType.setAdapter(goodsTypeAdapter);

		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);

		if (null != dataCollectCompeteInfo) {
			txtShaJiaSKUNum.setText(dataCollectCompeteInfo.getBismitnum());
			txtDisplayZb.setText(dataCollectCompeteInfo.getJpchenli());
			txtJPSKUId.setText(dataCollectCompeteInfo.getJpbismit());
			txtSKUId.setText(dataCollectCompeteInfo.getGhybismit());
			txtSale.setText(dataCollectCompeteInfo.getJpsaleprice());
			txtCuXSale.setText(dataCollectCompeteInfo.getJppromprice());
			txtCompeteInfo.setText(dataCollectCompeteInfo.getJpremark());
			spiCompeteBrand.setSelection(
					getSpinnerSelectedItemPosition(dataCollectCompeteInfo.getCompeteBrandInfo()),
					true);
			spiGoodsType.setSelection(
					getSpinnerSelectedItemPosition(dataCollectCompeteInfo.getGoodsType()), true);
			spiUnit.setSelection(
					getSpinnerSelectedItemPosition(dataCollectCompeteInfo.getUnitInfo()), true);
			btnStartDateModify.setText(dataCollectCompeteInfo.getJpbegintdate());
			btnEndDataModify.setText(dataCollectCompeteInfo.getJpenddate());
		} else {
			btnStartDateModify.setText(vsdate);
			btnEndDataModify.setText(vsdate);
			dataCollectCompeteInfo = new DataCollectCompeteInfoBean();
		}

	}

	private void addListener() {
		txtJPSKUId.setKeyListener(CommonUtil.charNumListener);
		txtJPSKUId.setOnFocusChangeListener(new OnFocusChangeListener() {
			public void onFocusChange(View v, boolean hasFocus) {
				String str = ((EditText) v).getText().toString();
				if (!hasFocus && !"".equals(str)) {
					txtJPSKUId.setText(str.toUpperCase());
				}
			}
		});
		txtSKUId.setKeyListener(CommonUtil.charNumListener);
		txtSKUId.setOnFocusChangeListener(new OnFocusChangeListener() {
			public void onFocusChange(View v, boolean hasFocus) {
				String str = ((EditText) v).getText().toString();
				if (!hasFocus && !"".equals(str)) {
					txtSKUId.setText(str.toUpperCase());
				}
			}
		});

		spiUnit.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				UnitInfoBean bean = (UnitInfoBean) parent.getItemAtPosition(position);
				dataCollectCompeteInfo.setUnitInfo(bean);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}

		});

		spiCompeteBrand.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				CompeteBrandInfoBean bean = (CompeteBrandInfoBean) parent
						.getItemAtPosition(position);
				dataCollectCompeteInfo.setCompeteBrandInfo(bean);

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}

		});

		spiGoodsType.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				GoodsTypeBean bean = (GoodsTypeBean) parent.getItemAtPosition(position);
				dataCollectCompeteInfo.setGoodsType(bean);

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}

		});

		btnStartDateModify.setOnClickListener(btnListenerEvent);
		btnEndDataModify.setOnClickListener(btnListenerEvent);
		btnSave.setOnClickListener(btnListenerEvent);
		btnBack.setOnClickListener(btnListenerEvent);
	}

	/**
	 * get data's
	 */
	private boolean getData() {
		String sKUIdStr = txtSKUId.getText().toString().trim();
		dataCollectCompeteInfo.setGhybismit(sKUIdStr.toUpperCase());
		dataCollectCompeteInfo.setJpbismit(txtJPSKUId.getText().toString().trim().toUpperCase());

		String displayZbString = txtDisplayZb.getText().toString().trim();
		if (null == displayZbString || "".equals(displayZbString)) {
			dataCollectCompeteInfo.setJpchenli("0");
		}
		dataCollectCompeteInfo.setJpchenli(displayZbString);

		String shaJiaSKUNumStr = txtShaJiaSKUNum.getText().toString().trim();
		if (null == shaJiaSKUNumStr || "".equals(shaJiaSKUNumStr)
				|| Integer.parseInt(shaJiaSKUNumStr) == 0) {
			Toast.makeText(DataCollectCompete.this, "请输入上架SKU数或者上架SKU数不能为0!", Toast.LENGTH_SHORT)
					.show();
			return false;
		}
		dataCollectCompeteInfo.setBismitnum(shaJiaSKUNumStr);

		String saleStr = txtSale.getText().toString().trim();
		dataCollectCompeteInfo.setJpsaleprice(saleStr);

		String cuXSaleStr = txtCuXSale.getText().toString().trim();
		dataCollectCompeteInfo.setJppromprice(cuXSaleStr);

		dataCollectCompeteInfo.setJpbegintdate(btnStartDateModify.getText().toString());
		dataCollectCompeteInfo.setJpenddate(btnEndDataModify.getText().toString());
		dataCollectCompeteInfo.setJpremark(txtCompeteInfo.getText().toString().trim());
		return true;

	}

	/**
	 * button listener event
	 */
	private View.OnClickListener btnListenerEvent = new View.OnClickListener() {

		public void onClick(View v) {
			Button button = (Button) v;
			switch (button.getId()) {
				case R.id.btnStartDateModify :
					showDialog(R.id.btnStartDateModify);
					break;
				case R.id.btnEndDataModify :
					showDialog(R.id.btnEndDataModify);
					break;
				case R.id.btnBack :
					DataCollectCompete.this.finish();
					break;
				case R.id.btnSave :
					if (!getData()) {
						return;
					}
					try {
						if (!checkDate(dataCollectCompeteInfo.getJpbegintdate(),
								dataCollectCompeteInfo.getJpenddate())) {
							return;
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					bundle = new Bundle();
					bundle.putSerializable("dataCollectCompeteInfo", dataCollectCompeteInfo);
					bundle.putInt("dataCollCompetePosition", dataCollCompetePosition);
					intent = new Intent();
					intent.putExtras(bundle);
					DataCollectCompete.this.setResult(RESULT_OK, intent);
					DataCollectCompete.this.finish();
					break;
			}
		}
	};

	private OnDateSetListener startDateLis = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			String monthStr = "01";
			String dayStr = "01";
			if (mMonth + 1 < 10) {
				monthStr = "0" + (mMonth + 1);
			} else {
				monthStr = (mMonth + 1) + "";
			}
			mDay = dayOfMonth;
			if (mDay < 10) {
				dayStr = "0" + mDay;
			} else {
				dayStr = mDay + "";
			}
			btnStartDateModify.setText(mYear + "-" + monthStr + "-" + dayStr);
		}
	};

	private OnDateSetListener endDateLis = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			String monthStr = "01";
			String dayStr = "01";
			if (mMonth + 1 < 10) {
				monthStr = "0" + (mMonth + 1);
			} else {
				monthStr = (mMonth + 1) + "";
			}
			mDay = dayOfMonth;
			if (mDay < 10) {
				dayStr = "0" + mDay;
			} else {
				dayStr = mDay + "";
			}
			btnEndDataModify.setText(mYear + "-" + monthStr + "-" + dayStr);
		}
	};

	private boolean checkDate(String startDateStr, String endDateStr) throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = format.parse(startDateStr);
		Date endDate = format.parse(endDateStr);
		int result = startDate.compareTo(endDate);
		if (result == 1) {
			Toast.makeText(DataCollectCompete.this, "开始日期必须小于结束日期!", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
			case R.id.btnStartDateModify :
				return new DatePickerDialog(this, startDateLis, mYear, mMonth, mDay);
			case R.id.btnEndDataModify :
				return new DatePickerDialog(this, endDateLis, mYear, mMonth, mDay);
		}
		return null;
	}

	private int getSpinnerSelectedItemPosition(UnitInfoBean unitInfo) {
		int position = 0;
		for (int i = 0; i < unitInfoList.size(); i++) {
			if (unitInfo.getUnit().equals(unitInfoList.get(i).getUnit())) {
				position = i;
			}
		}
		return position;
	}

	private int getSpinnerSelectedItemPosition(GoodsTypeBean goodsType) {
		int position = 0;
		for (int i = 0; i < goodsTypeList.size(); i++) {
			if (goodsType.getPlid().equals(goodsTypeList.get(i).getPlid())) {
				position = i;
			}
		}
		return position;
	}

	private int getSpinnerSelectedItemPosition(CompeteBrandInfoBean competeBrandInfo) {
		int position = 0;
		for (int i = 0; i < competeBrandInfoList.size(); i++) {
			if (competeBrandInfo.getJp().equals(competeBrandInfoList.get(i).getJp())) {
				position = i;
			}
		}
		return position;
	}
}
